package com.bruan.recursion;


import java.io.File;
import java.io.IOException;

public class RecursionTest3 {
    public static void main(String[] args) throws IOException {
        searchFile(new File("D:/Program"),"WeChat.exe");
    }

    /**
     * 去目录下搜索文件
     * @param dir
     * @param fileName
     */
    public static void searchFile(File dir,String fileName) throws IOException {
        if (dir == null||!dir.exists()||dir.isFile()){
            return;
        }else {
            File[] files = dir.listFiles();


            if (files!=null&&files.length>0){
                //遍历一级文件对象
                for (File file : files) {
                    if (file.isFile()){
                        if (file.getName().contains(fileName)){
                            System.out.println("found ["+fileName+"] in: "+file.getAbsolutePath());
                            //启动程序
                            Runtime runtime = Runtime.getRuntime();
                            runtime.exec(file.getAbsolutePath());
                        }
                    }else {
                        //遍历到文件夹，继续递归
                        searchFile(file,fileName);
                    }
                }
            }
        }
    }
}
